﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using RazakiBLL.Entities;
using RazakiBLL.Managers;

namespace RazakiBLL
{
    /// <summary>
    /// Unit of Work
    /// نکته: در هر کلاس تنها یک نمونه از این کلاس ساخته شود.
    /// </summary>
    public class UowRazaki : IDisposable
    {
        #region >>>CONSTRUCTOR<<<

        public UowRazaki()
        {
            var entityConnection = ConnectionStringManager.GetEDM();

            _context = new RazakiDBEntities(entityConnection);
        }

        #endregion

        #region >>>FIELD<<<

        private RazakiDBEntities _context;
        private bool _disposed;

        private NewTagRps _newTagRepository;
        private NewRps _newRepository;
        private ApplicationPropertyRps _applicationPropertyRepository;
        private AspnetRoleRps _aspnetRoleRepository;
        private ExceptionLogRps _exceptionLogRepository;
        private MessageAnonymousRps _messageAnonymousRepository;
        private MessageForumRps _messageForumRepository;
        private MessagePermissionRps _messagePermissionRepository;
        private MessageTypeRps _messageTypeRepository;
        private UserDetailRps _userDetailRepository;
        private OrderRealRps _orderRealRepository;
        private PaymentRps _paymentRepository;
        private BankRps _bankRepository;
        private WorkRps _workRepository;

        #endregion

        #region >>>REPOSITORY PROPERTY<<<

        public RazakiDBEntities Context
        {
            get { return _context; }
        }

        public NewTagRps NewTagRepository
        {
            get
            {
                if (_newTagRepository == null)
                {
                    _newTagRepository = new NewTagRps(_context);
                }

                return _newTagRepository;
            }
        }

        public NewRps NewRepository
        {
            get
            {
                if (_newRepository == null)
                {
                    _newRepository = new NewRps(_context);
                }

                return _newRepository;
            }
        }

        public ApplicationPropertyRps ApplicationPropertyRepository
        {
            get
            {
                if (_applicationPropertyRepository == null)
                {
                    _applicationPropertyRepository = new ApplicationPropertyRps(_context);
                }

                return _applicationPropertyRepository;
            }
        }

        public AspnetRoleRps AspnetRoleRepository
        {
            get
            {
                if (_aspnetRoleRepository == null)
                {
                    _aspnetRoleRepository = new AspnetRoleRps(_context);
                }

                return _aspnetRoleRepository;
            }
        }

        public ExceptionLogRps ExceptionLogRepository
        {
            get
            {
                if (_exceptionLogRepository == null)
                {
                    _exceptionLogRepository = new ExceptionLogRps(_context);
                }

                return _exceptionLogRepository;
            }
        }

        public MessageAnonymousRps MessageAnonymousRepository
        {
            get
            {
                if (_messageAnonymousRepository == null)
                {
                    _messageAnonymousRepository = new MessageAnonymousRps(_context);
                }

                return _messageAnonymousRepository;
            }
        }

        public MessageForumRps MessageForumRepository
        {
            get
            {
                if (_messageForumRepository == null)
                {
                    _messageForumRepository = new MessageForumRps(_context);
                }

                return _messageForumRepository;
            }
        }

        public MessagePermissionRps MessagePermissionRepository
        {
            get
            {
                if (_messagePermissionRepository == null)
                {
                    _messagePermissionRepository = new MessagePermissionRps(_context);
                }

                return _messagePermissionRepository;
            }
        }

        public MessageTypeRps MessageTypeRepository
        {
            get
            {
                if (_messageTypeRepository == null)
                {
                    _messageTypeRepository = new MessageTypeRps(_context);
                }

                return _messageTypeRepository;
            }
        }

        public UserDetailRps UserDetailRepository
        {
            get
            {
                if (_userDetailRepository == null)
                {
                    _userDetailRepository = new UserDetailRps(_context);
                }

                return _userDetailRepository;
            }
        }

        public OrderRealRps OrderRealRepository
        {
            get
            {
                if (_orderRealRepository == null)
                {
                    _orderRealRepository = new OrderRealRps(_context);
                }

                return _orderRealRepository;
            }
        }

        public PaymentRps PaymentRepository
        {
            get
            {
                if (_paymentRepository == null)
                {
                    _paymentRepository = new PaymentRps(_context);
                }

                return _paymentRepository;
            }
        }

        public BankRps BankRepository
        {
            get
            {
                if (_bankRepository == null)
                {
                    _bankRepository = new BankRps(_context);
                }

                return _bankRepository;
            }
        }

        public WorkRps WorkRepository
        {
            get
            {
                if (_workRepository == null)
                {
                    _workRepository = new WorkRps(_context);
                }

                return _workRepository;
            }
        }

        #endregion

        public int Save()
        {
            int result = UtilitiesBLL._DECLINE_ID;

            try
            {
                result = _context.SaveChanges();
            }
            catch (UpdateException exc)
            {
                ExceptionManager.Log(exc);
            }

            return result;
        }

        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }

            _disposed = true;
        }

    }
}
